package com.sub.controller;

import com.sub.model.SpFirstMenu;
import com.sub.model.SpSecondMenu;
import com.sub.model.SpThirdMenu;
import com.sub.service.FirstMenuService;
import com.sub.service.SecondMenuService;
import com.sub.service.ThirdMenuService;
import com.sub.utils.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("power")
public class PowerController {

	@Autowired
	private FirstMenuService firstMenuService;

	@Autowired
	private SecondMenuService secondMenuService;

	@Autowired
	private ThirdMenuService thirdMenuService;

	/**
	 * @Description: 展示power Tree
	 */
	@PostMapping("/tree")
	public JSONResult powertree() throws Exception {
		SpFirstMenu firstMenu = new SpFirstMenu("Y");
		List<SpFirstMenu> firstMenuList = firstMenuService.queryFirstMenuList(firstMenu);
		SpSecondMenu secondMenu = new SpSecondMenu("Y");
		List<SpSecondMenu> secondMenuList = secondMenuService.querySecondMenuList(secondMenu);
		SpThirdMenu thirdMenu = new SpThirdMenu("Y");
		List<SpThirdMenu> thirdMenuList = thirdMenuService.queryThirdMenuList(thirdMenu);

		for(SpSecondMenu spSecondMenu:secondMenuList){
			String secondId = spSecondMenu.getSpSecondMenuId();
			List<SpThirdMenu> thirdMenulist_ = new ArrayList<SpThirdMenu>();
			for (SpThirdMenu spThirdMenu:thirdMenuList){
               if (secondId.equals(spThirdMenu.getSpSecondMenuId().toString())){
               	 thirdMenulist_.add(spThirdMenu);
			   }
			}
			spSecondMenu.setChildren(thirdMenulist_);
		}

		for (SpFirstMenu spFirstMenu:firstMenuList){
			String firstId = spFirstMenu.getSpFirstMenuId();
			List<SpSecondMenu> secondMenulist_ = new ArrayList<SpSecondMenu>();
			for (SpSecondMenu spSecondMenu:secondMenuList){
				if (firstId.equals(spSecondMenu.getSpFirstMenuId().toString())){
					secondMenulist_.add(spSecondMenu);
				}
			}
			spFirstMenu.setChildren(secondMenulist_);
		}
        return JSONResult.ok(firstMenuList);
	}


}
